package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址管理模块相关接口
 *
 * @Author mr.Du
 * @Date 2025/6/3 2025/6/3
 */
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private AddressBookMapper addressBookMapper;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook) {
        addressBookService.add(addressBook);
        return Result.success();
    }

    /**
     * 查询地址列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result list() {
        List<AddressBook> addressBookList = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .list();
        return Result.success(addressBookList);
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public Result isDefault() {
        List<AddressBook> addressBookList = addressBookService.lambdaQuery()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault, 0)
                .list();
        return Result.success(addressBookList);
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }


    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam("id") Long id) {
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getId, id);
        addressBookMapper.delete(wrapper);
        return Result.success();
    }

    /**
     * 根据ID查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id) {
        AddressBook address = addressBookService.lambdaQuery()
                .eq(AddressBook::getId, id)
                .one();
        return Result.success(address);
    }


    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.lambdaUpdate()
                .eq(AddressBook::getUserId,BaseContext.getCurrentId())
                .set(AddressBook::getIsDefault, 0)
                .update();
        addressBookService.lambdaUpdate()
                .eq(AddressBook::getId, addressBook.getId())
                .set(AddressBook::getIsDefault, 1)
                .update();
        return Result.success();
    }
}
